﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using DataEditor.DataLayer;

namespace DataEditor.ViewModels
{
    public class UnitsViewModel : BaseViewModel
    {

        #region Constructors

        public UnitsViewModel()
        {
            MakeCommands();
        }

        #endregion

        #region Properties

        private ObservableCollection<UnitItemViewModel> _unitItems;

        public ObservableCollection<UnitItemViewModel> UnitItems
        {
            get
            {
                _unitItems = new ObservableCollection<UnitItemViewModel>();


                var query = from u in dataContext.UNITs
                            select u;
                foreach (var item in query)
                {
                    _unitItems.Add(new UnitItemViewModel(item));
                }

                return _unitItems;
            }
            set
            {
                _unitItems = value;
                NotifyOfPropertyChange(() => UnitItems);
            }
        }

        private UnitItemViewModel _selectedUnitItem;

        public UnitItemViewModel SelectedUnitItem
        {
            get { return _selectedUnitItem; }
            set
            {
                _selectedUnitItem = value;
                NotifyOfPropertyChange(() => SelectedUnitItem);
            }
        }


        private RelayCommand _addNewCommand;

        public RelayCommand AddNewCommand
        {
            get { return _addNewCommand; }
            set
            {
                _addNewCommand = value;
                NotifyOfPropertyChange(() => AddNewCommand);
            }
        }

        private RelayCommand _removeCommand;

        public RelayCommand RemoveCommand
        {
            get { return _removeCommand; }
            set { _removeCommand = value; }
        }


        #endregion

        #region Actions/methods

        private void MakeCommands()
        {
            _addNewCommand = new RelayCommand(
                param =>
                {
                    var newUnit = new UNIT()
                    {
                        UNIT_SYMBOL_FK = 1,
                        IsValid = true,
                        ValidFrom = DateTime.Now
                    };
                    dataContext.UNITs.AddObject(newUnit);
                    dataContext.SaveChanges();

                    UnitItems.Add(new UnitItemViewModel(newUnit));
                    NotifyOfPropertyChange(() => UnitItems);
                },
                (obj) =>
                {
                    return true;
                }
                );

            _removeCommand = new RelayCommand(
                param =>
                {
                    UnitItems.Remove(SelectedUnitItem);
                    dataContext.UNITs.DeleteObject(SelectedUnitItem.UnitModel);
                    dataContext.SaveChanges();
                    NotifyOfPropertyChange(() => UnitItems);
                },
                (obj) =>
                {
                    return SelectedUnitItem != null;
                }
                );

        }

        #endregion

        #region Actions/methods

        public void Save()
        {
            dataContext.SaveChanges();
        }

        #endregion
    }
}
